---
title: "Indivisibility Data Analysis"
author: "Vinh Pham"
date: '2024-4-28'
output:
  html_document: default
  pdf_document: default
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r results='asis'}
# library(haven) #read data
library(tinytex)
library(dplyr)
library(tidyverse) #function %>%
library(lme4)
library(stargazer)
library(ggplot2)
library(cowplot)
library(ggpubr) #combining graphs
library(psych)
library(dplyr)
library(car)
library(ggpubr)
library(vcd)
library(grid)
library(sjPlot)
```

## Notions 
RS stands for restricted subjects (those assigned a large unit number). \
FS stands for flexible subjects (those assigned 1 as the unit number). \

## Data setup 
```{r}
master <- read.csv('C:/Users/vinht/Desktop/Barg w Indv/Bargaining 2.csv')
data <- master %>% filter(Period>0,unit_me>1)
# As unit_me>1, the data is filter so that all of the "me" subjects are RS and opponents are FS.

data <- data %>%
  group_by(SubjectID) %>%
  mutate(avgpay = mean(money_me, na.rm = TRUE))

data$me_rb<-ifelse(data$unit_me>1,1,0)
data$nodeal<-ifelse(data$deal==0,1,0)

total_deal<-sum(data$deal==1)
total_nodeal<-sum(data$nodeal==1)
total_deal7<-sum(data$unit_me==7&data$deal==1)
total_deal8<-sum(data$unit_me==8&data$deal==1)
total_deal9<-sum(data$unit_me==9&data$deal==1)
total_deal4<-sum(data$unit_me==4&data$deal==1)
total_deal6<-sum(data$unit_me==6&data$deal==1)

total_barg<-nrow(data)
total_barg7<-sum(data$unit_me==7)
total_barg8<-sum(data$unit_me==8)
total_barg9<-sum(data$unit_me==9)
total_barg4<-sum(data$unit_me==4)
total_barg6<-sum(data$unit_me==6)

#Deal rates by treatment
total_deal/total_barg
total_deal7/total_barg7
total_deal8/total_barg8
total_deal9/total_barg9
total_deal4/total_barg4
total_deal6/total_barg6
```

```{r}
# Identify ED, NBS and strongly efficient allocations according to standard utility:

data$ED_me <- ifelse(data$unit_me == 7, 7,
                        ifelse(data$unit_me == 8, 8,
                        ifelse(data$unit_me == 9, 9,
                        ifelse(data$unit_me == 4, 8, 6))))
data$ED_opponent<- data$ED_me
data$ED_true<-ifelse(data$money_me==data$ED_me&data$money_opponent==data$ED_opponent,1,0) #when both the payoffs of me and opponent adhere to ED, the allocation is classified as ED

data$NBS_me <- NA 
data$NBS_opponent <- NA 
data$NBS_me2 <- NA 
data$NBS_opponent2 <- NA

data$NBS_me[data$unit_me == 7] <- 7 
data$NBS_opponent[data$unit_me == 7] <- 13
data$NBS_me[data$unit_me == 8] <- 8 
data$NBS_opponent[data$unit_me == 8] <- 12
data$NBS_me[data$unit_me == 9] <- 9 
data$NBS_opponent[data$unit_me == 9] <- 11
data$NBS_me[data$unit_me == 6] <- 12 
data$NBS_opponent[data$unit_me == 6] <- 8
data$NBS_me[data$unit_me == 4] <- 12 
data$NBS_opponent[data$unit_me == 4] <- 8
data$NBS_me2[data$unit_me == 4] <- 8 
data$NBS_opponent2[data$unit_me == 4] <- 12
data$NBS_true <- ifelse(
  rowSums(cbind(
    data$money_me == data$NBS_me & data$money_opponent == data$NBS_opponent,
    data$money_me == data$NBS_me2 & data$money_opponent == data$NBS_opponent2
  ), na.rm = TRUE) > 0,
  1, 0)
 #when both the payoffs of me and opponent adhere to NBS, the allocation is classified as NBS

data$StrEff_me <- NA 
data$StrEff_opponent <- NA 
data$StrEff_me2 <- NA 
data$StrEff_opponent2 <- NA
data$StrEff_me3 <- NA 
data$StrEff_opponent3 <- NA
data$StrEff_me4 <- NA 
data$StrEff_opponent4 <- NA

data$StrEff_me[data$unit_me == 7] <- 7 
data$StrEff_opponent[data$unit_me == 7] <- 13
data$StrEff_me2[data$unit_me == 7] <- 14
data$StrEff_opponent2[data$unit_me == 7] <- 6

data$StrEff_me[data$unit_me == 8] <- 8 
data$StrEff_opponent[data$unit_me == 8] <- 12
data$StrEff_me2[data$unit_me == 8] <- 16 
data$StrEff_opponent2[data$unit_me == 8] <- 4

data$StrEff_me[data$unit_me == 9] <- 9 
data$StrEff_opponent[data$unit_me == 9] <- 11

data$StrEff_me[data$unit_me == 6] <- 12 
data$StrEff_opponent[data$unit_me == 6] <- 8
data$StrEff_me2[data$unit_me == 6] <- 6 
data$StrEff_opponent2[data$unit_me == 6] <- 14
data$StrEff_me3[data$unit_me == 6] <- 18 
data$StrEff_opponent3[data$unit_me == 6] <- 2

data$StrEff_me[data$unit_me == 4] <- 12 
data$StrEff_opponent[data$unit_me == 4] <- 8
data$StrEff_me2[data$unit_me == 4] <- 8 
data$StrEff_opponent2[data$unit_me == 4] <- 12
data$StrEff_me3[data$unit_me == 4] <- 0 
data$StrEff_opponent3[data$unit_me == 4] <- 20
data$StrEff_me4[data$unit_me == 4] <- 20 
data$StrEff_opponent4[data$unit_me == 4] <- 0

# Flag whether the deal matches either StrEff allocation
data$StrEff_true <- ifelse(
  rowSums(cbind(
    data$money_me == data$StrEff_me & data$money_opponent == data$StrEff_opponent,
    data$money_me == data$StrEff_me2 & data$money_opponent == data$StrEff_opponent2,
    data$money_me == data$StrEff_me3 & data$money_opponent == data$StrEff_opponent3,
    data$money_me == data$StrEff_me4 & data$money_opponent == data$StrEff_opponent4
  ), na.rm = TRUE) > 0,
  1, 0)
 #when both the payoffs of me and opponent adhere to strong efficiency, the allocation is classified as StrEff

```
```{r}
total_ED<-sum(data$ED_true)
total_NBS <- sum(data$NBS_true)
total_StrEff <- sum(data$StrEff_true)
total_ED/total_deal
total_NBS/total_deal
total_StrEff/total_deal

data7<-filter(data,unit_me==7)
data8<-filter(data,unit_me==8)
data9<-filter(data,unit_me==9)
data4<-filter(data,unit_me==4)
data6<-filter(data,unit_me==6)

```
```{r}
#Two-proportion Z-test
prop.test(x = c(496, 116), n = c(741, 741), alternative = "greater")

```

# Calculate frequency for each allocation (Figure 3)
```{r}
#T9
pct911<-100*sum(data$money_me==9&data$money_opponent==11)/total_barg9
pct910<-100*sum(data$money_me==9&data$money_opponent==10)/total_barg9
pct99<-100*sum(data$money_me==9&data$money_opponent==9)/total_barg9
pct98<-100*sum(data$money_me==9&data$money_opponent==8)/total_barg9
pct90<-100*sum(data$money_me==9&data$money_opponent==0)/total_barg9
pct911
pct910
pct99
pct98
pct90 

100*(total_deal9/total_barg9) #deal percentage
100*(1-total_deal9/total_barg9) #non-deal percentage
```
```{r}
#T8
pct812_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==12)/total_barg8
pct811_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==11)/total_barg8
pct810_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==10)/total_barg8
pct89_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==9)/total_barg8
pct88_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==8)/total_barg8
pct87_u8<-100*sum(data$unit_me==8&data$money_me==8&data$money_opponent==7)/total_barg8
pct164_u8<-100*sum(data$unit_me==8&data$money_me==16&data$money_opponent==4)/total_barg8
pct812_u8
pct811_u8
pct810_u8
pct89_u8
pct88_u8
pct87_u8
pct164_u8

100*(total_deal8/total_barg8) #deal percentage
100*(1-total_deal8/total_barg8) #non-deal percentage
```

```{r}
#T7
pct713<-100*sum(data$money_me==7&data$money_opponent==13)/total_barg7
pct712<-100*sum(data$money_me==7&data$money_opponent==12)/total_barg7
pct711<-100*sum(data$money_me==7&data$money_opponent==11)/total_barg7
pct710<-100*sum(data$money_me==7&data$money_opponent==10)/total_barg7
pct79<-100*sum(data$money_me==7&data$money_opponent==9)/total_barg7
pct78<-100*sum(data$money_me==7&data$money_opponent==8)/total_barg7
pct77<-100*sum(data$money_me==7&data$money_opponent==7)/total_barg7
pct76<-100*sum(data$money_me==7&data$money_opponent==6)/total_barg7
pct146<-100*sum(data$money_me==14&data$money_opponent==6)/total_barg7
pct713
pct712
pct711
pct710
pct79
pct78
pct77
pct76
pct146

100*(total_deal7/total_barg7) #deal percentage
100*(1-total_deal7/total_barg7) #non-deal percentage

#deal rate for (14,6)
pct146_u7deal<-100*sum(data$unit_me==7&data$money_me==14&data$money_opponent==6)/total_deal7
pct146_u7deal
```
```{r}
#T4
pct812_u4<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==12)/total_barg4
pct128_u4<-100*sum(data$unit_me==4&data$money_me==12&data$money_opponent==8)/total_barg4
pct88_u4<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==8)/total_barg4
pct89_u4<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==9)/total_barg4
pct810_u4<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==10)/total_barg4
pct811_u4<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==11)/total_barg4
pct020_u4<-100*sum(data$unit_me==4&data$money_me==0&data$money_opponent==20)/total_barg4
pct200_u4<-100*sum(data$unit_me==4&data$money_me==20&data$money_opponent==0)/total_barg4

pct812_u4
pct128_u4
pct88_u4
pct89_u4
pct810_u4
pct811_u4
pct020_u4
pct200_u4

100*(total_deal4/total_barg4) #deal percentage
100*(1-total_deal4/total_barg4) #non-deal percentage

#breakdown of deal rates for 2 NBS in T4
pct812_u4deal<-100*sum(data$unit_me==4&data$money_me==8&data$money_opponent==12)/total_deal4
pct128_u4deal<-100*sum(data$unit_me==4&data$money_me==12&data$money_opponent==8)/total_deal4
pct812_u4deal
pct128_u4deal
```
```{r}
#T6
pct128_u6<-100*sum(data$unit_me==6&data$money_me==12&data$money_opponent==8)/total_barg6
pct66_u6<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==6)/total_barg6
pct68_u6<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==8)/total_barg6
pct69_u6<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==9)/total_barg6
pct610_u6<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==10)/total_barg6
pct614_u6<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==14)/total_barg6
pct180_u6<-100*sum(data$unit_me==6&data$money_me==18&data$money_opponent==0)/total_barg6
pct182_u6<-100*sum(data$unit_me==6&data$money_me==18&data$money_opponent==2)/total_barg6

pct128_u6
pct66_u6
pct68_u6
pct69_u6
pct610_u6
pct614_u6
pct180_u6
pct182_u6

100*(total_deal6/total_barg6) #deal percentage
100*(1-total_deal6/total_barg6) #non-deal percentage

#deal rate for (12,8)
pct614_u6deal<-100*sum(data$unit_me==6&data$money_me==6&data$money_opponent==14)/total_deal6
pct614_u6deal
```

#Calculate CI in all outcomes (Table 2)
##Overall
```{r}
p_hatED <- mean(data$ED_true)
n <- length(data$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data$NBS_true)
n <- sum(!is.na(data$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data$StrEff_true)
n <- sum(!is.na(data$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data$nodeal)
n <- sum(!is.na(data$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```
##T7
```{r}
p_hatED <- mean(data7$ED_true)
n <- length(data7$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data7$NBS_true)
n <- sum(!is.na(data7$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data7$StrEff_true)
n <- sum(!is.na(data7$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data7$nodeal)
n <- sum(!is.na(data7$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```
##T8
```{r}
p_hatED <- mean(data8$ED_true)
n <- length(data8$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data8$NBS_true)
n <- sum(!is.na(data8$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data8$StrEff_true)
n <- sum(!is.na(data8$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data8$nodeal)
n <- sum(!is.na(data8$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```
##T9
```{r}
p_hatED <- mean(data9$ED_true)
n <- length(data9$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data9$NBS_true)
n <- sum(!is.na(data9$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data9$StrEff_true)
n <- sum(!is.na(data9$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data9$nodeal)
n <- sum(!is.na(data9$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```
##T4
```{r}
p_hatED <- mean(data4$ED_true)
n <- length(data4$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data4$NBS_true)
n <- sum(!is.na(data4$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data4$StrEff_true)
n <- sum(!is.na(data4$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data4$nodeal)
n <- sum(!is.na(data4$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```
##T6
```{r}
p_hatED <- mean(data6$ED_true)
n <- length(data6$ED_true)
se <- sqrt(p_hatED * (1 - p_hatED) / n)
z_score <- 1.96
lower_boundED <- p_hatED - z_score * se
upper_boundED <- p_hatED + z_score * se
cat("Sample Proportion:", p_hatED, "\n")
cat("95% Confidence Interval:", lower_boundED, "to", upper_boundED, "\n")

p_hatNBS <- mean(data6$NBS_true)
n <- sum(!is.na(data6$NBS_true))
se <- sqrt(p_hatNBS * (1 - p_hatNBS) / n)
z_score <- 1.96
lower_boundNBS <- p_hatNBS - z_score * se
upper_boundNBS <- p_hatNBS + z_score * se
cat("Sample Proportion:", p_hatNBS, "\n")
cat("95% Confidence Interval:", lower_boundNBS, "to", upper_boundNBS, "\n")

p_hatStrEff <- mean(data6$StrEff_true)
n <- sum(!is.na(data6$StrEff_true))
se <- sqrt(p_hatStrEff * (1 - p_hatStrEff) / n)
z_score <- 1.96
lower_boundStrEff <- p_hatStrEff - z_score * se
upper_boundStrEff <- p_hatStrEff + z_score * se
cat("Sample Proportion:", p_hatStrEff, "\n")
cat("95% Confidence Interval:", lower_boundStrEff, "to", upper_boundStrEff, "\n")

p_hatnodeal <- mean(data6$nodeal)
n <- sum(!is.na(data6$nodeal))
se <- sqrt(p_hatnodeal * (1 - p_hatnodeal) / n)
z_score <- 1.96
lower_boundnodeal <- p_hatnodeal - z_score * se
upper_boundnodeal <- p_hatnodeal + z_score * se
cat("Sample Proportion:", p_hatnodeal, "\n")
cat("95% Confidence Interval:", lower_boundnodeal, "to", upper_boundnodeal, "\n")
```


# Mosaic plot NBS, ED vs others (Figure 4)
## Count allocation types
```{r}

total_ED_T7<-sum(subset(data,unit_me==7)$ED_true)
total_NBS_T7<-sum(subset(data,unit_me==7)$NBS_true)
total_StrEff_T7<-sum(subset(data,unit_me==7)$StrEff_true)
total_bargaining_T7<-sum(count(subset(data,unit_me==7)))
total_nodeal_T7<-sum(subset(data,unit_me==7)$nodeal==1)
 
total_ED_T8<-sum(subset(data,unit_me==8)$ED_true)
total_NBS_T8<-sum(subset(data,unit_me==8)$NBS_true)
total_StrEff_T8<-sum(subset(data,unit_me==8)$StrEff_true)
total_bargaining_T8<-sum(count(subset(data,unit_me==8)))
total_nodeal_T8<-sum(subset(data,unit_me==8)$nodeal==1)
 
total_ED_T9<-sum(subset(data,unit_me==9)$ED_true)
total_NBS_T9<-sum(subset(data,unit_me==9)$NBS_true)
total_StrEff_T9<-sum(subset(data,unit_me==9)$StrEff_true)
total_bargaining_T9<-sum(count(subset(data,unit_me==9)))
total_nodeal_T9<-sum(subset(data,unit_me==9)$nodeal==1)

total_ED_T4<-sum(subset(data,unit_me==4)$ED_true)
total_NBS_T4<-sum(subset(data,unit_me==4)$NBS_true)
total_StrEff_T4<-sum(subset(data,unit_me==4)$StrEff_true)
total_bargaining_T4<-sum(count(subset(data,unit_me==4)))
total_nodeal_T4<-sum(subset(data,unit_me==4)$nodeal==1)

total_ED_T6<-sum(subset(data,unit_me==6)$ED_true)
total_NBS_T6<-sum(subset(data,unit_me==6)$NBS_true)
total_StrEff_T6<-sum(subset(data,unit_me==6)$StrEff_true)
total_bargaining_T6<-sum(count(subset(data,unit_me==6)))
total_nodeal_T6<-sum(subset(data,unit_me==6)$nodeal==1)

```


```{r}
dat4b <- data.frame(
  "Treatment" = c(
    "T4", "T6", "T7", "T8", "T9",  # ED
    "T4", "T6", "T7", "T8", "T9",  # aOther
    "T4", "T6", "T7", "T8", "T9",  # aNodeal
    "T4", "T6", "T7", "T8", "T9"   # NBS
  ),
  "Allocation" = c(
    rep("ED", 5),
    rep("aOther", 5),
    rep("aNodeal", 5),
    rep("NBS", 5)
  ),
  "Proportion" = c(
    # ED
    total_ED_T4 / total_barg4,
    total_ED_T6 / total_barg6,
    total_ED_T7 / total_barg7,
    total_ED_T8 / total_barg8,
    total_ED_T9 / total_barg9,

    # aOther
(total_barg4 - total_ED_T4 - total_NBS_T4 - total_nodeal_T4) / total_barg4,
(total_barg6 - total_ED_T6 - total_NBS_T6 - total_nodeal_T6) / total_barg6,
(total_barg7 - total_ED_T7 - total_NBS_T7 - total_nodeal_T7) / total_barg7,
(total_barg8 - total_ED_T8 - total_NBS_T8 - total_nodeal_T8) / total_barg8,
(total_barg9 - total_ED_T9 - total_NBS_T9 - total_nodeal_T9) / total_barg9,

    # aNodeal
    total_nodeal_T4 / total_barg4,
    total_nodeal_T6 / total_barg6,
    total_nodeal_T7 / total_barg7,
    total_nodeal_T8 / total_barg8,
    total_nodeal_T9 / total_barg9,
    
    # NBS
    total_NBS_T4 / total_barg4,
    total_NBS_T6 / total_barg6,
    total_NBS_T7 / total_barg7,
    total_NBS_T8 / total_barg8,
    total_NBS_T9 / total_barg9
  )
)

dat4b$Proportion <- round(dat4b$Proportion * 100)
dat4b$Treatment <- factor(dat4b$Treatment, levels = c("T7", "T8", "T9", "T4", "T6"))

# Plot
mos4b <- ggplot(dat4b,
       aes(x = Treatment, y = Proportion, fill = Allocation)) +
  geom_col(colour = "black", width = 0.75) +
  geom_text(aes(label = Proportion), 
            position = position_stack(vjust = 0.5), 
            size = rel(9)) +
  scale_color_identity() + 
  theme_void()

treatdist1<- mos4b +
  theme(
    axis.text.x = element_text(size = rel(2)),
    legend.title = element_blank(),
    legend.text = element_text(size = rel(2)),
    legend.position = "none"
  ) +
  scale_fill_manual(
    values = c("grey","white", "#DAF1F3", "skyblue3"),
    labels = c("No agreement","Others", "Equal division", "NBS")
  )
treatdist1
```

# Mosaic plot StrEff, ED vs others (Figure 4)
```{r}
dat4b <- data.frame(
  "Treatment" = c(
    "T4", "T6", "T7", "T8", "T9",  # ED
    "T4", "T6", "T7", "T8", "T9",  # aOther
    "T4", "T6", "T7", "T8", "T9",  # aNodeal
    "T4", "T6", "T7", "T8", "T9"   # StrEff
  ),
  "Allocation" = c(
    rep("ED", 5),
    rep("aOther", 5),
    rep("aNodeal", 5),
    rep("StrEff", 5)
  ),
  "Proportion" = c(
    # ED
    total_ED_T4 / total_barg4,
    total_ED_T6 / total_barg6,
    total_ED_T7 / total_barg7,
    total_ED_T8 / total_barg8,
    total_ED_T9 / total_barg9,

    # aOther
(total_barg4 - total_ED_T4 - total_StrEff_T4 - total_nodeal_T4) / total_barg4,
(total_barg6 - total_ED_T6 - total_StrEff_T6 - total_nodeal_T6) / total_barg6,
(total_barg7 - total_ED_T7 - total_StrEff_T7 - total_nodeal_T7) / total_barg7,
(total_barg8 - total_ED_T8 - total_StrEff_T8 - total_nodeal_T8) / total_barg8,
(total_barg9 - total_ED_T9 - total_StrEff_T9 - total_nodeal_T9) / total_barg9,

    # aNodeal
    total_nodeal_T4 / total_barg4,
    total_nodeal_T6 / total_barg6,
    total_nodeal_T7 / total_barg7,
    total_nodeal_T8 / total_barg8,
    total_nodeal_T9 / total_barg9,
    
    # StrEff
    total_StrEff_T4 / total_barg4,
    total_StrEff_T6 / total_barg6,
    total_StrEff_T7 / total_barg7,
    total_StrEff_T8 / total_barg8,
    total_StrEff_T9 / total_barg9
  )
)

dat4b$Proportion <- round(dat4b$Proportion * 100)
dat4b$Treatment <- factor(dat4b$Treatment, levels = c("T7", "T8", "T9", "T4", "T6"))

mos4b <- ggplot(dat4b,
       aes(x = Treatment, y = Proportion, fill = Allocation)) +
  geom_col(colour = "black", width = 0.75) +
  geom_text(aes(label = Proportion), 
            position = position_stack(vjust = 0.5), 
            size = rel(9)) +
  scale_color_identity() +
  theme_void()

treatdist2<-mos4b +
  theme(
    axis.text.x = element_text(size = rel(2)),
    legend.title = element_blank(),
    legend.text = element_text(size = rel(2)),
    legend.position = "none"
  ) +
  scale_fill_manual(
    values = c("grey","white", "#DAF1F3", "darkslategray4"),
    labels = c("No agreement", "Others", "Equal division", "Strongly efficient")
  )
treatdist2
```

# Figure 4 legend
```{r}
legend_grob <- grobTree({
  values <- c("skyblue3", "darkslategray4", "grey", "#DAF1F3", "white")
  labels <- c("NBS", "Strongly efficient", "No agreement", "Equal division", "Other agreements")
  
  n_col <- 3
  n_row <- 2
  h_spacing <- 0.25
  v_spacing <- 0.05
  
  grobs <- list()
  
  for (i in seq_along(values)) {
    col_index <- (i - 1) %% n_col
    row_index <- (i - 1) %/% n_col
    
    x_box <- unit(0.15 + col_index * h_spacing, "npc")
    y_box <- unit(0.7 - row_index * v_spacing, "npc")
    
    grobs[[length(grobs) + 1]] <- rectGrob(x = x_box, y = y_box,
                                           width = unit(0.035, "npc"), height = unit(1.0, "lines"),
                                           gp = gpar(fill = values[i], col = "black"))
    
    grobs[[length(grobs) + 1]] <- textGrob(labels[i], x = x_box + unit(0.03, "npc"), y = y_box,
                                           just = "left", gp = gpar(fontsize = 14))
  }
  
  do.call(gList, grobs)
})

```
```{r}
ggsave("treatdist1.png", plot = treatdist1, width = 7, height = 5)
ggsave("treatdist2.png", plot = treatdist2, width = 7, height = 5)
ggsave("treatdistlegend.png", plot = legend_grob, width = 14, height = 5)

```

## Main regression 
```{r}
datadeal<-filter(data,deal==1)

#Setup additional variables
datadeal$NBSgap<- datadeal$NBSproddiff <- abs(
  ifelse(datadeal$unit_me == 7, (13-7) / 13,
  ifelse(datadeal$unit_me %in% c(4, 6, 8), (12-8) / 12,
         (11-9) / 11))
)#gap between RB and FB embedded in NBS

datadeal$EDdom<-ifelse(datadeal$unit_me==6,1,0)
datadeal$NBSno<-ifelse(datadeal$unit_me==4,1,0)
datadeal$supp<-ifelse(datadeal$unit_me==4|datadeal$unit_me==6,1,0)
  
data789<-filter(datadeal,unit_me %in% c(7,8,9))
data46<-filter(datadeal,unit_me %in% c(4,6))

```


# Table 4: Determinants of bargaining outcomes (random effect for each session)

```{r}

EDLog <- datadeal %>%  glmer(ED_true ~ NBSgap+NBSno+EDdom+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
NBSLog <- datadeal %>%  glmer(NBS_true ~ NBSgap+NBSno+EDdom+SVO_me+SVO_opponent+gender_me +gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
SELog <- datadeal %>%  glmer(StrEff_true ~ NBSgap+NBSno+EDdom+SVO_me+SVO_opponent+gender_me +gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
EarningsOpp <- datadeal %>%  lmer(money_opponent ~ NBSgap+NBSno+EDdom+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = .)
EarningsMe <- datadeal %>%  lmer(money_me ~ NBSgap+NBSno+EDdom+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = .)


CombiLog <- list(EDLog,NBSLog,SELog,EarningsOpp,EarningsMe)
stargazer(CombiLog,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "Double NBS","ED dominated", "SVO of RB", "SVO of FB","Male RB","Male FB", "Phase"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001)
          )

```

```{r}
## Reporting p-values

stargazer(CombiLog,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "Double NBS","ED dominated","SVO of RB", "SVO of FB", "Male RB","Male FB", "Phase"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001), report=('vc*p')
          )

```


#Table B.1: T7 8 9 only
```{r}

EDLog789 <- data789 %>%  glmer(ED_true ~ NBSgap+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
NBSLog789 <- data789 %>%  glmer(NBS_true ~ NBSgap+SVO_me+SVO_opponent+gender_me +gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
SELog789 <- data789 %>%  glmer(StrEff_true ~ NBSgap+SVO_me+SVO_opponent+gender_me +gender_opponent+ Phase+(1| SessionID), data = ., family = binomial(link = "logit"))
EarningsOpp789 <- data789 %>%  lmer(money_opponent ~ NBSgap+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = .)
EarningsMe789 <- data789 %>%  lmer(money_me ~ NBSgap+SVO_me+SVO_opponent+gender_me+gender_opponent+ Phase+(1| SessionID), data = .)


CombiLog789 <- list(EDLog789,NBSLog789,SELog789,EarningsOpp789,EarningsMe789)
stargazer(CombiLog789,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "SVO of RB", "SVO of FB", "Male RB","Male FB", "Phase"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001)
          )
```
```{r}
## Reporting p-values

stargazer(CombiLog789,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "SVO of RB", "SVO of FB", "Male RB","Male FB", "Phase"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001),report=('vc*p')
          )
```


## Table B.2: IA regression

```{r}
dataIA <- datadeal %>% filter(SubjectID>399,Alpha_me<2&Beta_me<2&Beta_me>=0&Alpha_opponent<2&Beta_opponent<2&Beta_opponent>=0)

EDLogIA <- dataIA %>%  glmer(ED_true ~ NBSgap+NBSno+EDdom+Alpha_me+Alpha_opponent+Beta_me+Beta_opponent+gender_opponent+ (1| SessionID), data = ., family = binomial(link = "logit"))
NBSLogIA <- dataIA %>%  glmer(NBS_true ~ NBSgap+NBSno+EDdom+Alpha_me+Alpha_opponent+Beta_me+Beta_opponent +gender_opponent+ (1| SessionID), data = ., family = binomial(link = "logit"))
SELogIA <- dataIA %>%  glmer(StrEff_true ~ NBSgap+NBSno+EDdom+Alpha_me+Alpha_opponent+Beta_me+Beta_opponent +gender_opponent+ (1| SessionID), data = ., family = binomial(link = "logit"))
EarningsOppIA <- dataIA %>%  lmer(money_opponent ~ NBSgap+NBSno+EDdom+Alpha_me+Alpha_opponent+Beta_me+Beta_opponent+gender_opponent+ (1| SessionID), data = .)
EarningsMeIA <- dataIA %>%  lmer(money_me ~ NBSgap+NBSno+EDdom+Alpha_me+Alpha_opponent+Beta_me+Beta_opponent+gender_opponent+ (1| SessionID), data = .)


CombiLogIA <- list(EDLogIA,NBSLogIA,SELogIA,EarningsOppIA,EarningsMeIA)
stargazer(CombiLogIA,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "Double NBS","ED dominated","Alpha of RB", "Alpha of FB","Beta of RB","Beta of FB", "Male FB"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001)
          )

```

```{r}
## Reporting p-values

CombiLogIA <- list(EDLogIA,NBSLogIA,SELogIA,EarningsOppIA,EarningsMeIA)
stargazer(CombiLogIA,
          dep.var.caption = "Bargaining agreements",
          dep.var.labels=c("ED","NBS","StrEff","Payoff of FB","Payoff of RB"),
          covariate.labels = c("NBS Gap", "Double NBS","ED dominated","Alpha of RB", "Alpha of FB","Beta of RB","Beta of FB", "Male FB"),
          notes.label = "Significance level",
          type = "text",
          keep.stat = c("n", "rsq"), 
          star.cutoffs = c(0.05, 0.01, 0.001), report=('vc*p')
          )

```


## Distribution of SVO parameters (Table 3)
```{r}
dataSVO <- read.csv('C:/Users/vinht/Desktop/Barg w Indv/SVO2.csv')
describe(dataSVO$SVO_parameter)
describeBy(dataSVO$SVO_parameter,group=dataSVO$svo_type)
describeBy(dataSVO$SVO_parameter,group=dataSVO$Gender)

describeBy(dataSVO$SVO_parameter, group=dataSVO$svo_type) 
SVOIndi<-dataSVO %>% filter(svo_type == 3)
medianIndi <-median(SVOIndi$SVO_parameter)
meanIndi <-mean(SVOIndi$SVO_parameter)

SVOPros<-dataSVO %>% filter(svo_type == 2)
medianPros <-median(SVOPros$SVO_parameter)
meanPros <-mean(SVOPros$SVO_parameter)
medianIndi
medianPros
meanIndi
meanPros
```

# Distribution of SVO measures (Figure 5)
```{r}
ggplot(dataSVO, aes(x=svo_angle)) +theme_bw() + 
    geom_histogram(aes(y = (..count..)/sum(..count..)),binwidth=1.75, alpha=5, colour="navyblue",position="identity", fill="skyblue3") + geom_vline(aes(xintercept=22.45,color="red"))+ geom_vline(aes(xintercept=-12.04,color="red"))+ geom_vline(aes(xintercept=57.15,color="red"))+ coord_cartesian(xlim = c(-30, 75),ylim = c(0, 0.48))+ geom_hline(aes(yintercept=0))+ scale_y_continuous(labels=scales::percent)+ labs(x = "SVO angle",size=rel(50))+ labs(y = "Frequency",size=rel(5))+ geom_density(color="darkgrey",size=1)+ annotate("text", x = -24, y = 0.46, label = "Competitive",size=rel(5))  + annotate("text", x = 5, y = 0.46, label = "Individualistic",size=rel(5)) + annotate("text", x = 40, y = 0.46, label = "Prosocial",size=rel(5))  + annotate("text", x = 70, y = 0.46, label = "Altruistic",size=rel(5))  + theme(legend.title= element_blank(),legend.text = element_blank(),legend.key =  element_blank(),text = element_text(size=rel(4)))

```

### Correlation SVO - gender
```{r}
dataSVO <- dataSVO %>%
  left_join(data %>% select(SubjectID, gender_me) %>% distinct(), by = "SubjectID")

summary(dataSVO$avgpay[dataSVO$svo_type == 2])
summary(dataSVO$avgpay[dataSVO$svo_type == 3])

cor.test(dataSVO$SVO_parameter, dataSVO$gender_me, method = c("pearson", "kendall", "spearman"))

```

# Inequality aversion parameters
## Data setup
```{r}
dataIA <- read.csv('C:/Users/vinht/Desktop/Barg w Indv/IA2.csv')
dataIA1<- dataIA %>% filter(Alpha < 2 & Beta < 2&Beta>=0)
dataIA1$Alpha<-as.numeric(dataIA1$Alpha)
dataIA1$Beta<-as.numeric(dataIA1$Beta)



dataIA1 <- dataIA1 %>%
  left_join(dataSVO %>% select(SubjectID, svo_type) %>% distinct(), by = "SubjectID")

dataIA1 <- dataIA1 %>%
  left_join(dataSVO %>% select(SubjectID, SVO_parameter) %>% distinct(), by = "SubjectID")

dataIA1 <- dataIA1 %>%
  group_by(Alpha,Beta,svo_type) %>%
  mutate(ABcount = n())

dataIA1$BlargerA<-ifelse(dataIA1$Alpha<=dataIA1$Beta,0,1)
describe(dataIA1$BlargerA)
```

## Distribution of IA parameters (Figure A.4)
```{r}

iaplot <- ggplot(dataIA1, aes(Alpha, Beta)) +geom_point(aes(size=ABcount),colour="salmon")+theme(axis.text.x=element_text(size=rel(1.5)),axis.title.x=element_text(size=rel(1.5)),axis.text.y=element_text(size=rel(1.5)),axis.title.y=element_text(size=rel(1.5)),legend.title= element_blank(),legend.text = element_text(size=rel(1.5)))+scale_y_continuous(limits=c(-0.1,0.8)) 

iaplot2<-iaplot+ geom_vline(xintercept = 0) + geom_hline(yintercept=0)+   scale_size(range = c(2, 15),breaks=c(1,10,20))

ggsave("iaplot2.png", plot = iaplot2, width = 7, height = 5)

```

##Describe IA by SVO (Table 3)
```{r}
describe(dataIA1$Alpha)
describeBy(as.numeric(dataIA1$Alpha),group=dataIA1$svo_type)
describe(dataIA1$Beta)
describeBy(as.numeric(dataIA1$Beta),group=dataIA1$svo_type)
```

## Correlation tests IA and SVO
```{r}
cor.test(dataIA1$SVO_parameter, dataIA1$Alpha, method = c("pearson", "kendall", "spearman"))
cor.test(dataIA1$SVO_parameter, dataIA1$Beta, method = c("pearson", "kendall", "spearman"))
```

#Bargaining process (Table 5)
```{r}
#Data setup
allproposals <- read.csv('C:/Users/vinht/Desktop/Barg w Indv/Proposals 4.csv')

allproposals <- allproposals %>%
  left_join(data %>% select(GroupID, SubjectID), by = "GroupID") %>% rename(RB = SubjectID)

allproposals <- allproposals %>%
  left_join(data %>% select(GroupID, deal), by = "GroupID") %>%
  rename(Deal = deal)


allproposals <- allproposals %>%
  left_join(data %>% select(GroupID, OpponentID), by = "GroupID") %>%
  rename(FB = OpponentID)

allproposals$RB_proposer <-ifelse(allproposals$ProposerID==allproposals$RB,1,0)
allproposals$FB_proposer <-ifelse(allproposals$ProposerID==allproposals$FB,1,0)

allproposals <- allproposals %>%
  left_join(data %>% select(GroupID, money_me, money_opponent), by = "GroupID") %>%
  rename(PayoffRB = money_me, PayoffFB = money_opponent)

allproposals$Totalproposals <- ave(allproposals$GroupID, allproposals$GroupID, FUN = length)

allproposals <- allproposals %>%
  arrange(desc(Proposaltime)) %>%
  group_by(GroupID) %>%
  mutate(First = if_else(row_number() == 1, 1, 0)) %>%
  ungroup()

allproposals <- allproposals %>%
  arrange(desc(Proposaltime)) %>%
  group_by(GroupID) %>%
  mutate(Agreetolast = if_else(row_number() == n() & Deal == 1, 1, 0)) %>%
  ungroup()

allproposals$IsDeal <- ifelse(
  (allproposals$RB_proposer == 1 &
   allproposals$propose_money_me_me == allproposals$PayoffRB &
   allproposals$propose_money_opponent_me == allproposals$PayoffFB) |
  (allproposals$RB_proposer == 0 &
   allproposals$propose_money_me_me == allproposals$PayoffFB &
   allproposals$propose_money_opponent_me == allproposals$PayoffRB),
  1,
  0
)

allproposals <- allproposals %>%
  arrange(desc(Proposaltime)) %>%
  group_by(GroupID) %>%
  mutate(Agreetolast = if_else(row_number() == n() & IsDeal == 1, 1, 0)) %>%
  ungroup()

allproposals <- allproposals %>%
  mutate(
    Dealproposer = case_when(
      Agreetolast == 1 & RB_proposer == 1 ~ FB,
      Agreetolast == 1 & RB_proposer == 0 ~ RB,
      TRUE ~ NA_real_
    )
  )

allproposals <- allproposals %>%
  group_by(GroupID) %>%
  mutate(
    Dealproposer = first(Dealproposer[!is.na(Dealproposer)])
  ) %>%
  ungroup()


allproposals <- allproposals %>%
  group_by(GroupID) %>%
  mutate(
    PropDeal = if_else(
      Dealproposer == ProposerID & Proposaltime == min(Proposaltime[Dealproposer == ProposerID]),
      1L,
      0L
    )
  ) %>%
  ungroup()

allproposals$PropDeal[is.na(allproposals$PropDeal)] <- 0

allproposals <- allproposals %>%
  arrange(desc(Proposaltime)) %>%
  group_by(GroupID) %>%
  mutate(LastProp = if_else(
    (row_number() == n() & IsDeal == 0) | (PropDeal == 1 & Agreetolast == 0),
    1, 0
  )) %>%
  ungroup()
 #identify last proposals, including for non-deals



```
```{r}
proposals <- allproposals %>% filter(Agreetolast != 1) #excluding agreement to the last proposal (clicking on the same cell).

proposals$ED<-ifelse(proposals$propose_money_me_me==proposals$propose_money_opponent_me,1,0)

proposals$NBS <- ifelse(
  (proposals$Treatment == 7 &
   proposals$propose_money_me_me == 7 &
   proposals$propose_money_opponent_me == 13 &
   proposals$RB_proposer == 1) |
  (proposals$Treatment == 7 &
   proposals$propose_money_me_me == 13 &
   proposals$propose_money_opponent_me == 7 &
   proposals$RB_proposer == 0) |
  (proposals$Treatment == 8 &
   proposals$propose_money_me_me == 8 &
   proposals$propose_money_opponent_me == 12 &
   proposals$RB_proposer == 1) |
  (proposals$Treatment == 8 &
   proposals$propose_money_me_me == 12 &
   proposals$propose_money_opponent_me == 8 &
   proposals$RB_proposer == 0) |
  (proposals$Treatment == 9 &
   proposals$propose_money_me_me == 9 &
   proposals$propose_money_opponent_me == 11 &
   proposals$RB_proposer == 1) |
  (proposals$Treatment == 9 &
   proposals$propose_money_me_me == 11 &
   proposals$propose_money_opponent_me == 9 &
   proposals$RB_proposer == 0) |
  (proposals$Treatment == 6 &
   proposals$propose_money_me_me == 12 &
   proposals$propose_money_opponent_me == 8 &
   proposals$RB_proposer == 1) |
  (proposals$Treatment == 6 &
   proposals$propose_money_me_me == 8 &
   proposals$propose_money_opponent_me == 12 &
   proposals$RB_proposer == 0) |
  (proposals$Treatment == 4 &
   proposals$propose_money_me_me == 8 &
   proposals$propose_money_opponent_me == 12) |
  (proposals$Treatment == 4 &
   proposals$propose_money_me_me == 12 &
   proposals$propose_money_opponent_me == 8),
  1,
  0
)

proposals$StrEff<-ifelse(proposals$propose_money_me_me+proposals$propose_money_opponent_me==20,1,0)


proposals$NBSFB <- ifelse(proposals$NBS==1&proposals$FB_proposer==1,1,0)
proposals$NBSRB <- ifelse(proposals$NBS==1&proposals$FB_proposer==0,1,0)

proposals$EDFB <- ifelse(proposals$ED==1&proposals$FB_proposer==1,1,0)
proposals$EDRB <- ifelse(proposals$ED==1&proposals$FB_proposer==0,1,0)

proposals$StrEffFB <- ifelse(proposals$StrEff==1&proposals$FB_proposer==1,1,0)
proposals$StrEffRB <- ifelse(proposals$StrEff==1&proposals$FB_proposer==0,1,0)

proposals$FirstFB<- ifelse(proposals$First==1&proposals$FB_proposer==1,1,0)
proposals$FirstRB<- ifelse(proposals$First==1&proposals$FB_proposer==0,1,0)
proposals$FirstNBS<- ifelse(proposals$First==1&proposals$NBS==1,1,0)
proposals$FirstED<- ifelse(proposals$First==1&proposals$ED==1,1,0)
proposals$FirstStrEff<- ifelse(proposals$First==1&proposals$StrEff==1,1,0)


proposals$LastPropFB<- ifelse(proposals$LastProp==1&proposals$FB_proposer==1,1,0)
proposals$LastPropRB<- ifelse(proposals$LastProp==1&proposals$FB_proposer==0,1,0)
proposals$LastPropNBS<- ifelse(proposals$LastProp==1&proposals$NBS==1,1,0)
proposals$LastPropED<- ifelse(proposals$LastProp==1&proposals$ED==1,1,0)
proposals$LastPropStrEff<- ifelse(proposals$LastProp==1&proposals$StrEff==1,1,0)


proposals$PropDealFB<- ifelse(proposals$PropDeal==1&proposals$FB_proposer==1,1,0)
proposals$PropDealRB<- ifelse(proposals$PropDeal==1&proposals$FB_proposer==0,1,0)
proposals$PropDealNBS<- ifelse(proposals$PropDeal==1&proposals$NBS==1,1,0)
proposals$PropDealED<- ifelse(proposals$PropDeal==1&proposals$ED==1,1,0)
proposals$PropDealStrEff<- ifelse(proposals$PropDeal==1&proposals$StrEff==1,1,0)


proposals$TotalproposalsFB <- ifelse(proposals$Totalproposals==1&proposals$FB_proposer==1,1,0)
proposals$TotalproposalsRB <- ifelse(proposals$Totalproposals==1&proposals$FB_proposer==0,1,0)

proposals7 <- proposals %>% filter(Treatment==7)
proposals8 <- proposals %>% filter(Treatment==8)
proposals9 <- proposals %>% filter(Treatment==9)
proposals4 <- proposals %>% filter(Treatment==4)
proposals6 <- proposals %>% filter(Treatment==6)

proposedeals <- proposals %>% filter(Deal==1)

proposedeals7 <- proposedeals %>% filter(Treatment==7)
proposedeals8 <- proposedeals %>% filter(Treatment==8)
proposedeals9 <- proposedeals %>% filter(Treatment==9)
proposedeals4 <- proposedeals %>% filter(Treatment==4)
proposedeals6 <- proposedeals %>% filter(Treatment==6)
```
## Who proposed ED
```{r}
# Overall
pctED.RB <- round(100 * sum(proposals$EDRB) / sum(proposals$ED), 2)
pctED.RB
pctED.FB <- round(100 * sum(proposals$EDFB) / sum(proposals$ED), 2)
pctED.FB

# Treatment 7
pctED.RB7 <- round(100 * sum(proposals7$EDRB) / sum(proposals7$ED), 2)
pctED.RB7
pctED.FB7 <- round(100 * sum(proposals7$EDFB) / sum(proposals7$ED), 2)
pctED.FB7

# Treatment 8
pctED.RB8 <- round(100 * sum(proposals8$EDRB) / sum(proposals8$ED), 2)
pctED.RB8
pctED.FB8 <- round(100 * sum(proposals8$EDFB) / sum(proposals8$ED), 2)
pctED.FB8

# Treatment 9
pctED.RB9 <- round(100 * sum(proposals9$EDRB) / sum(proposals9$ED), 2)
pctED.RB9
pctED.FB9 <- round(100 * sum(proposals9$EDFB) / sum(proposals9$ED), 2)
pctED.FB9

# Treatment 4
pctED.RB4 <- round(100 * sum(proposals4$EDRB) / sum(proposals4$ED), 2)
pctED.RB4
pctED.FB4 <- round(100 * sum(proposals4$EDFB) / sum(proposals4$ED), 2)
pctED.FB4

# Treatment 6
pctED.RB6 <- round(100 * sum(proposals6$EDRB) / sum(proposals6$ED), 2)
pctED.RB6
pctED.FB6 <- round(100 * sum(proposals6$EDFB) / sum(proposals6$ED), 2)
pctED.FB6
```

## Who proposed NBS
```{r}
# Overall
pctNBS.RB <- round(100 * sum(proposals$NBSRB) / sum(proposals$NBS), 2)
pctNBS.RB
pctNBS.FB <- round(100 * sum(proposals$NBSFB) / sum(proposals$NBS), 2)
pctNBS.FB

# Treatment 7
pctNBS.RB7 <- round(100 * sum(proposals7$NBSRB) / sum(proposals7$NBS), 2)
pctNBS.RB7
pctNBS.FB7 <- round(100 * sum(proposals7$NBSFB) / sum(proposals7$NBS), 2)
pctNBS.FB7

# Treatment 8
pctNBS.RB8 <- round(100 * sum(proposals8$NBSRB) / sum(proposals8$NBS), 2)
pctNBS.RB8
pctNBS.FB8 <- round(100 * sum(proposals8$NBSFB) / sum(proposals8$NBS), 2)
pctNBS.FB8

# Treatment 9
pctNBS.RB9 <- round(100 * sum(proposals9$NBSRB) / sum(proposals9$NBS), 2)
pctNBS.RB9
pctNBS.FB9 <- round(100 * sum(proposals9$NBSFB) / sum(proposals9$NBS), 2)
pctNBS.FB9

# Treatment 4
pctNBS.RB4 <- round(100 * sum(proposals4$NBSRB) / sum(proposals4$NBS), 2)
pctNBS.RB4
pctNBS.FB4 <- round(100 * sum(proposals4$NBSFB) / sum(proposals4$NBS), 2)
pctNBS.FB4

# Treatment 6
pctNBS.RB6 <- round(100 * sum(proposals6$NBSRB) / sum(proposals6$NBS), 2)
pctNBS.RB6
pctNBS.FB6 <- round(100 * sum(proposals6$NBSFB) / sum(proposals6$NBS), 2)
pctNBS.FB6
```

## Who proposed StrEff
```{r}
# Overall
pctStrEff.RB <- round(100 * sum(proposals$StrEffRB) / sum(proposals$StrEff), 2)
pctStrEff.RB
pctStrEff.FB <- round(100 * sum(proposals$StrEffFB) / sum(proposals$StrEff), 2)
pctStrEff.FB

# Treatment 7
pctStrEff.RB7 <- round(100 * sum(proposals7$StrEffRB) / sum(proposals7$StrEff), 2)
pctStrEff.RB7
pctStrEff.FB7 <- round(100 * sum(proposals7$StrEffFB) / sum(proposals7$StrEff), 2)
pctStrEff.FB7

# Treatment 8
pctStrEff.RB8 <- round(100 * sum(proposals8$StrEffRB) / sum(proposals8$StrEff), 2)
pctStrEff.RB8
pctStrEff.FB8 <- round(100 * sum(proposals8$StrEffFB) / sum(proposals8$StrEff), 2)
pctStrEff.FB8

# Treatment 9
pctStrEff.RB9 <- round(100 * sum(proposals9$StrEffRB) / sum(proposals9$StrEff), 2)
pctStrEff.RB9
pctStrEff.FB9 <- round(100 * sum(proposals9$StrEffFB) / sum(proposals9$StrEff), 2)
pctStrEff.FB9

# Treatment 4
pctStrEff.RB4 <- round(100 * sum(proposals4$StrEffRB) / sum(proposals4$StrEff), 2)
pctStrEff.RB4
pctStrEff.FB4 <- round(100 * sum(proposals4$StrEffFB) / sum(proposals4$StrEff), 2)
pctStrEff.FB4

# Treatment 6
pctStrEff.RB6 <- round(100 * sum(proposals6$StrEffRB) / sum(proposals6$StrEff), 2)
pctStrEff.RB6
pctStrEff.FB6 <- round(100 * sum(proposals6$StrEffFB) / sum(proposals6$StrEff), 2)
pctStrEff.FB6
```
## Who proposed first
```{r}
# Overall
pctFirst.RB <- round(100 * sum(proposals$FirstRB) / sum(proposals$First), 2)
pctFirst.RB
pctFirst.FB <- round(100 * sum(proposals$FirstFB) / sum(proposals$First), 2)
pctFirst.FB

# Treatment 7
pctFirst.RB7 <- round(100 * sum(proposals7$FirstRB) / sum(proposals7$First), 2)
pctFirst.RB7
pctFirst.FB7 <- round(100 * sum(proposals7$FirstFB) / sum(proposals7$First), 2)
pctFirst.FB7

# Treatment 8
pctFirst.RB8 <- round(100 * sum(proposals8$FirstRB) / sum(proposals8$First), 2)
pctFirst.RB8
pctFirst.FB8 <- round(100 * sum(proposals8$FirstFB) / sum(proposals8$First), 2)
pctFirst.FB8

# Treatment 9
pctFirst.RB9 <- round(100 * sum(proposals9$FirstRB) / sum(proposals9$First), 2)
pctFirst.RB9
pctFirst.FB9 <- round(100 * sum(proposals9$FirstFB) / sum(proposals9$First), 2)
pctFirst.FB9

# Treatment 4
pctFirst.RB4 <- round(100 * sum(proposals4$FirstRB) / sum(proposals4$First), 2)
pctFirst.RB4
pctFirst.FB4 <- round(100 * sum(proposals4$FirstFB) / sum(proposals4$First), 2)
pctFirst.FB4

# Treatment 6
pctFirst.RB6 <- round(100 * sum(proposals6$FirstRB) / sum(proposals6$First), 2)
pctFirst.RB6
pctFirst.FB6 <- round(100 * sum(proposals6$FirstFB) / sum(proposals6$First), 2)
pctFirst.FB6


```
## Type of opening proposals
```{r}

# FirstED
pctFirst.ED <- round(100 * sum(proposals$FirstED) / sum(proposals$First), 2)
pctFirst.ED
pctFirst.ED7 <- round(100 * sum(proposals7$FirstED) / sum(proposals7$First), 2)
pctFirst.ED7
pctFirst.ED8 <- round(100 * sum(proposals8$FirstED) / sum(proposals8$First), 2)
pctFirst.ED8
pctFirst.ED9 <- round(100 * sum(proposals9$FirstED) / sum(proposals9$First), 2)
pctFirst.ED9
pctFirst.ED4 <- round(100 * sum(proposals4$FirstED) / sum(proposals4$First), 2)
pctFirst.ED4
pctFirst.ED6 <- round(100 * sum(proposals6$FirstED) / sum(proposals6$First), 2)
pctFirst.ED6

# FirstNBS
pctFirst.NBS <- round(100 * sum(proposals$FirstNBS) / sum(proposals$First), 2)
pctFirst.NBS
pctFirst.NBS7 <- round(100 * sum(proposals7$FirstNBS) / sum(proposals7$First), 2)
pctFirst.NBS7
pctFirst.NBS8 <- round(100 * sum(proposals8$FirstNBS) / sum(proposals8$First), 2)
pctFirst.NBS8
pctFirst.NBS9 <- round(100 * sum(proposals9$FirstNBS) / sum(proposals9$First), 2)
pctFirst.NBS9
pctFirst.NBS4 <- round(100 * sum(proposals4$FirstNBS) / sum(proposals4$First), 2)
pctFirst.NBS4
pctFirst.NBS6 <- round(100 * sum(proposals6$FirstNBS) / sum(proposals6$First), 2)
pctFirst.NBS6


# FirstStrEff
pctFirst.StrEff <- round(100 * sum(proposals$FirstStrEff) / sum(proposals$First), 2)
pctFirst.StrEff
pctFirst.StrEff7 <- round(100 * sum(proposals7$FirstStrEff) / sum(proposals7$First), 2)
pctFirst.StrEff7
pctFirst.StrEff8 <- round(100 * sum(proposals8$FirstStrEff) / sum(proposals8$First), 2)
pctFirst.StrEff8
pctFirst.StrEff9 <- round(100 * sum(proposals9$FirstStrEff) / sum(proposals9$First), 2)
pctFirst.StrEff9
pctFirst.StrEff4 <- round(100 * sum(proposals4$FirstStrEff) / sum(proposals4$First), 2)
pctFirst.StrEff4
pctFirst.StrEff6 <- round(100 * sum(proposals6$FirstStrEff) / sum(proposals6$First), 2)
pctFirst.StrEff6

# FirstOthers
pctFirst.Others <- 100 - pctFirst.ED - pctFirst.StrEff
pctFirst.Others
pctFirst.Others7 <- 100 - pctFirst.ED7 - pctFirst.StrEff7
pctFirst.Others7
pctFirst.Others8 <- 100 - pctFirst.ED8 - pctFirst.StrEff8
pctFirst.Others8
pctFirst.Others9 <- 100 - pctFirst.ED9 - pctFirst.StrEff9
pctFirst.Others9
pctFirst.Others4 <- 100 - pctFirst.ED4 - pctFirst.StrEff4
pctFirst.Others4
pctFirst.Others6 <- 100 - pctFirst.ED6 - pctFirst.StrEff6
pctFirst.Others6
```

## Who proposed last
```{r}
# Overall
pctLastProp.RB <- round(100 * sum(proposals$LastPropRB) / sum(proposals$LastProp), 2)
pctLastProp.RB
pctLastProp.FB <- round(100 * sum(proposals$LastPropFB) / sum(proposals$LastProp), 2)
pctLastProp.FB

# Treatment 7
pctLastProp.RB7 <- round(100 * sum(proposals7$LastPropRB) / sum(proposals7$LastProp), 2)
pctLastProp.RB7
pctLastProp.FB7 <- round(100 * sum(proposals7$LastPropFB) / sum(proposals7$LastProp), 2)
pctLastProp.FB7

# Treatment 8
pctLastProp.RB8 <- round(100 * sum(proposals8$LastPropRB) / sum(proposals8$LastProp), 2)
pctLastProp.RB8
pctLastProp.FB8 <- round(100 * sum(proposals8$LastPropFB) / sum(proposals8$LastProp), 2)
pctLastProp.FB8

# Treatment 9
pctLastProp.RB9 <- round(100 * sum(proposals9$LastPropRB) / sum(proposals9$LastProp), 2)
pctLastProp.RB9
pctLastProp.FB9 <- round(100 * sum(proposals9$LastPropFB) / sum(proposals9$LastProp), 2)
pctLastProp.FB9

# Treatment 4
pctLastProp.RB4 <- round(100 * sum(proposals4$LastPropRB) / sum(proposals4$LastProp), 2)
pctLastProp.RB4
pctLastProp.FB4 <- round(100 * sum(proposals4$LastPropFB) / sum(proposals4$LastProp), 2)
pctLastProp.FB4

# Treatment 6
pctLastProp.RB6 <- round(100 * sum(proposals6$LastPropRB) / sum(proposals6$LastProp), 2)
pctLastProp.RB6
pctLastProp.FB6 <- round(100 * sum(proposals6$LastPropFB) / sum(proposals6$LastProp), 2)
pctLastProp.FB6

```
## Type of last proposals 
```{r}

# LastProp ED
pctLastProp.ED <- round(100 * sum(proposals$LastPropED) / sum(proposals$LastProp), 2)
pctLastProp.ED
pctLastProp.ED7 <- round(100 * sum(proposals7$LastPropED) / sum(proposals7$LastProp), 2)
pctLastProp.ED7
pctLastProp.ED8 <- round(100 * sum(proposals8$LastPropED) / sum(proposals8$LastProp), 2)
pctLastProp.ED8
pctLastProp.ED9 <- round(100 * sum(proposals9$LastPropED) / sum(proposals9$LastProp), 2)
pctLastProp.ED9
pctLastProp.ED4 <- round(100 * sum(proposals4$LastPropED) / sum(proposals4$LastProp), 2)
pctLastProp.ED4
pctLastProp.ED6 <- round(100 * sum(proposals6$LastPropED) / sum(proposals6$LastProp), 2)
pctLastProp.ED6

# LastProp NBS
pctLastProp.NBS <- round(100 * sum(proposals$LastPropNBS) / sum(proposals$LastProp), 2)
pctLastProp.NBS
pctLastProp.NBS7 <- round(100 * sum(proposals7$LastPropNBS) / sum(proposals7$LastProp), 2)
pctLastProp.NBS7
pctLastProp.NBS8 <- round(100 * sum(proposals8$LastPropNBS) / sum(proposals8$LastProp), 2)
pctLastProp.NBS8
pctLastProp.NBS9 <- round(100 * sum(proposals9$LastPropNBS) / sum(proposals9$LastProp), 2)
pctLastProp.NBS9
pctLastProp.NBS4 <- round(100 * sum(proposals4$LastPropNBS) / sum(proposals4$LastProp), 2)
pctLastProp.NBS4
pctLastProp.NBS6 <- round(100 * sum(proposals6$LastPropNBS) / sum(proposals6$LastProp), 2)
pctLastProp.NBS6

# LastProp StrEff
pctLastProp.StrEff <- round(100 * sum(proposals$LastPropStrEff) / sum(proposals$LastProp), 2)
pctLastProp.StrEff
pctLastProp.StrEff7 <- round(100 * sum(proposals7$LastPropStrEff) / sum(proposals7$LastProp), 2)
pctLastProp.StrEff7
pctLastProp.StrEff8 <- round(100 * sum(proposals8$LastPropStrEff) / sum(proposals8$LastProp), 2)
pctLastProp.StrEff8
pctLastProp.StrEff9 <- round(100 * sum(proposals9$LastPropStrEff) / sum(proposals9$LastProp), 2)
pctLastProp.StrEff9
pctLastProp.StrEff4 <- round(100 * sum(proposals4$LastPropStrEff) / sum(proposals4$LastProp), 2)
pctLastProp.StrEff4
pctLastProp.StrEff6 <- round(100 * sum(proposals6$LastPropStrEff) / sum(proposals6$LastProp), 2)
pctLastProp.StrEff6

# LastProp Others
pctLastProp.Others <- 100 - pctLastProp.ED - pctLastProp.StrEff
pctLastProp.Others
pctLastProp.Others7 <- 100 - pctLastProp.ED7 - pctLastProp.StrEff7
pctLastProp.Others7
pctLastProp.Others8 <- 100 - pctLastProp.ED8 - pctLastProp.StrEff8
pctLastProp.Others8
pctLastProp.Others9 <- 100 - pctLastProp.ED9 - pctLastProp.StrEff9
pctLastProp.Others9
pctLastProp.Others4 <- 100 - pctLastProp.ED4 - pctLastProp.StrEff4
pctLastProp.Others4
pctLastProp.Others6 <- 100 - pctLastProp.ED6 - pctLastProp.StrEff6
pctLastProp.Others6
```

## Number of proposals (before deal)
```{r}


# Overall
NumProposals.RB <- round(sum(proposedeals$RB_proposer) / total_deal, 2)
NumProposals.RB
NumProposals.FB <- round(sum(proposedeals$FB_proposer) / total_deal, 2)
NumProposals.FB

# Treatment 7
NumProposals.RB7 <- round(sum(proposedeals7$RB_proposer) / total_deal7, 2)
NumProposals.RB7
NumProposals.FB7 <- round(sum(proposedeals7$FB_proposer) / total_deal7, 2)
NumProposals.FB7

# Treatment 8
NumProposals.RB8 <- round(sum(proposedeals8$RB_proposer) / total_deal8, 2)
NumProposals.RB8
NumProposals.FB8 <- round(sum(proposedeals8$FB_proposer) / total_deal8, 2)
NumProposals.FB8

# Treatment 9
NumProposals.RB9 <- round(sum(proposedeals9$RB_proposer) / total_deal9, 2)
NumProposals.RB9
NumProposals.FB9 <- round(sum(proposedeals9$FB_proposer) / total_deal9, 2)
NumProposals.FB9

# Treatment 4
NumProposals.RB4 <- round(sum(proposedeals4$RB_proposer) / total_deal4, 2)
NumProposals.RB4
NumProposals.FB4 <- round(sum(proposedeals4$FB_proposer) / total_deal4, 2)
NumProposals.FB4

# Treatment 6
NumProposals.RB6 <- round(sum(proposedeals6$RB_proposer) / total_deal6, 2)
NumProposals.RB6
NumProposals.FB6 <- round(sum(proposedeals6$FB_proposer) / total_deal6, 2)
NumProposals.FB6

# Total
NumProposals <- NumProposals.FB + NumProposals.RB
NumProposals7 <- NumProposals.FB7 + NumProposals.RB7
NumProposals8 <- NumProposals.FB8 + NumProposals.RB8
NumProposals9 <- NumProposals.FB9 + NumProposals.RB9
NumProposals4 <- NumProposals.FB4 + NumProposals.RB4
NumProposals6 <- NumProposals.FB6 + NumProposals.RB6

NumProposals
NumProposals7
NumProposals8
NumProposals9
NumProposals4
NumProposals6
```
## Deal rate and bargaining time (from master data)
```{r}
#data includes nondeals, data includes only deals
data$dealpct<-data$deal*100
describe(data$dealpct)
describeBy(data$dealpct,group=data$unit_me)
```
```{r}
proposalsfinal<-allproposals %>% filter(Agreetolast==1)
proposalsfinal$barg_time<-180-proposalsfinal$Proposaltime
describe(proposalsfinal$barg_time)
describeBy(proposalsfinal$barg_time,group=proposalsfinal$Treatment)
```

